home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / INTERNET / MANDEL.ZIP / MANDEL.JAV < prev   
Text File  |  1996-01-18  |  4KB  |  130 lines

  1. /* @(#)Mandel.java 1.0 1/18/96 Darryll Truchan 
  2.  *
  3.  * Copyright (c) 1996 Darryll Truchan. All Rights Reserved.
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software
  6.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  7.  * without fee is hereby granted. 
  8.  *
  9.  */
  10.  
  11. import java.applet.Applet;
  12. import java.awt.Graphics;
  13. import java.awt.Color;
  14.  
  15. class Complex {
  16.     private double real, imag;
  17.     public Complex(double real, double imag) {
  18.         this.real = real; this.imag = imag;
  19.     }
  20.     public static Complex add(Complex a, Complex b) {
  21.         return new Complex(a.real + b.real,a.imag + b.imag);
  22.     }
  23.     public static Complex mul(Complex a, Complex b) {
  24.         return new Complex((a.real * b.real) - (a.imag * b.imag),(a.real * b.imag) + (a.imag * b.real));
  25.     }
  26.     public static double norm(Complex c) {
  27.         return (c.real * c.real) + (c.imag * c.imag);
  28.     }
  29. }
  30.  
  31. /**
  32.  *  A simple Mandelbrot fractal applet.
  33.  *
  34.  * @version 1.0
  35.  * @author Darryll Truchan
  36.  */
  37. public class Mandel extends Applet {
  38.  
  39.     private double left, top, right, bottom;
  40.     private double xtemp, ytemp, deltaX, deltaY;
  41.     private int iterations, bailout, width, height;
  42.     private Color colors[][];
  43.     private boolean done;
  44.  
  45.     public void init() {
  46.         done = false;
  47.         left = (new Double(getParameter("LEFT"))).doubleValue();
  48.         top = (new Double(getParameter("TOP"))).doubleValue();
  49.         right = (new Double(getParameter("RIGHT"))).doubleValue();
  50.         bottom = (new Double(getParameter("BOTTOM"))).doubleValue();
  51.          
  52.         iterations = (new Integer(getParameter("ITERATIONS"))).intValue();
  53.         bailout = (new Integer(getParameter("BAILOUT"))).intValue();
  54.         width = (new Integer(getParameter("WIDTH"))).intValue();
  55.         height = (new Integer(getParameter("HEIGHT"))).intValue();
  56.  
  57.         resize(width,height);
  58.         deltaX = (right-left)/width;
  59.         deltaY = (bottom-top)/height;
  60.  
  61.         colors = new Color[width][height];
  62.     }
  63.  
  64.     public void start() {
  65.         repaint();
  66.     }
  67.  
  68.     public void update(Graphics g) {
  69.         paint(g);
  70.     }
  71.  
  72.     private Color Calculate(double X, double Y) {
  73.         Complex C = new Complex(X,Y);
  74.         Complex Z = new Complex(0,0);
  75.         int cnt = 0;
  76.         do {
  77.             Z = Complex.mul(Z,Z);
  78.             Z = Complex.add(Z,C);
  79.             cnt++;
  80.         } while((cnt<iterations)&&(Complex.norm(Z)<bailout));
  81.         if(cnt == iterations) cnt = 8;
  82.         switch(cnt%8) {
  83.             case 0 : return new Color(0x00000000);
  84.             case 1 : return new Color(0x000000FF);            
  85.             case 2 : return new Color(0x0000FF00);
  86.             case 3 : return new Color(0x0000FFFF);
  87.             case 4 : return new Color(0x00FF0000);            
  88.             case 5 : return new Color(0x00FF00FF);
  89.             case 6 : return new Color(0x00FFFF00);
  90.             case 7 : return new Color(0x00FFFFFF);
  91.             default: return new Color(0x00000000);
  92.         }
  93.     }     
  94.  
  95.     public void paint(Graphics g) {
  96.         ytemp = top;
  97.         for(int y=0; y<height; y++) {
  98.             xtemp = left;
  99.             for(int x=0; x<width; x++) {                
  100.                 if(done == false) 
  101.                     colors[x][y] = Calculate(xtemp,ytemp);                
  102.                 g.setColor(colors[x][y]);
  103.                 g.drawLine(x,y,x,y);
  104.                 xtemp += deltaX;
  105.             }
  106.             ytemp += deltaY;
  107.         }
  108.         done = true;
  109.     }
  110.  
  111.     public String[][] getParameterInfo() {
  112.         String info[][] = {
  113.             {"Left", "double","Left bound of fractal"},
  114.             {"Top", "double","Right bound of fractal"},
  115.             {"Right", "double","Top bound of fractal"},
  116.             {"Bottom", "double","Bottom bound of fractal"},
  117.             {"Iterations", "int","Number of tests before infinity"},
  118.             {"Bailout", "int","Bailout to infinity"},
  119.             {"Width", "int","Width of the image"},
  120.             {"Height", "int","Height of the image"}
  121.         };
  122.         return info;
  123.     }
  124.  
  125.     public String getAppletInfo() {
  126.         return "Mandel.java 1.0\nDarryll Truchan\nCopyright(c) 1996\nAll Rights Reseved\n";
  127.     }
  128.  
  129. }
  130.